/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.ingsoc2;

import com.mycompany.ingsoc2.SwingViews.VcLoginViewSwing;
import com.mycompany.ingsoc2.SwingViews.VcPrincipalViewSwing;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 *
 * @author sebastian
 */
public class App {

    private Properties propiedadesApp = new Properties();
    private Connection conexion;
    FactoryDAO fDAO = null;
    private static App instance = null;

    /**
     *
     * @param args
     * @throws SQLException
     */
    public static void main(String args[]) throws SQLException {
        App myApp = App.getInstance();
        myApp.cargarPropiedades();
        if (myApp.dbInit()) {
            myApp.mostrarLogin();

        } else {
            System.out.println("La conexion no pudo abrirse. Verifique el archivo de configuracion.");
        }
    }

    /**
     * getInstance
     *
     * @return
     */
    public static App getInstance() {
        if (instance == null) {
            instance = new App();
        }
        return instance;
    }

    /**
     * cargarPropiedades de un archivo
     *
     * @return
     */
    public boolean cargarPropiedades() {
        InputStream isArchivo = this.getClass().getResourceAsStream("/config/configuracion.properties");

        try {
            this.propiedadesApp.load(isArchivo);
            isArchivo.close();
            return true;

        } catch (FileNotFoundException ex) {
            System.out.println(ex);
            return false;
        } catch (IOException ex) {
            System.out.println(ex);
            return false;
        }

    }

    /**
     * dbConnect
     *
     * @return
     */
    public boolean dbInit() {
        ModelDB instanceDB;
        try {

            if (propiedadesApp.getProperty("db.motor").equalsIgnoreCase("HSQL")) {
                fDAO = new FactoryHSQL();
                this.conexion = fDAO.createConnection(propiedadesApp.getProperty("db.string"), propiedadesApp.getProperty("db.usuario"), propiedadesApp.getProperty("db.password"));
                instanceDB = new ModelDBHSQL();
                instanceDB.createModel(this.conexion);
                return !this.conexion.isClosed();

            } else if (propiedadesApp.getProperty("db.motor").equalsIgnoreCase("MYSQL")) {
                fDAO = new FactoryMySQL();
                this.conexion = fDAO.createConnection(propiedadesApp.getProperty("db.string"), propiedadesApp.getProperty("db.usuario"), propiedadesApp.getProperty("db.password"));
                instanceDB = new ModelDBMYSQL();
                instanceDB.createModel(this.conexion);
                return !this.conexion.isClosed();
            }
        } catch (Exception ex) {
            System.out.println(ex);
            return false;
        }
        return false;

    }

    /**
     * mostrarLogin
     */
    public void mostrarLogin() throws SQLException {

        VcPrincipalModel vcpm = new VcPrincipalModel(fDAO.createDAOPelicula(conexion),fDAO.createDAOGenero(conexion));
        VcPrincipalViewSwing vcpvw = new VcPrincipalViewSwing();
        VcPrincipalController vcpc = new VcPrincipalController(vcpvw, vcpm);

        VcLoginModel vclm = new VcLoginModel();
        VcLoginViewSwing vclvw = new VcLoginViewSwing();
        VcLoginController vclc = new VcLoginController(vclvw, vclm, vcpc);

        vclc.mostrarLogin();

    }

    /**
     * getConn
     *
     * @return Devuelve la conexion
     */
    public Connection getConn() {
        return this.conexion;

    }
}
